//    Copyright (C) Kherty.  All rights reserved.
using System.Text;

namespace OpenLS.Spreadsheet.Parsing
{
    /// <summary>
    /// Summary description for ExplicitStringCreator.
    /// </summary>
    class ExplicitStringCreator : TokenCreator
    {
        public override char[] FirstChars()
        {
            return new char[] { '"' };
        }
        public override Token Match(Lexer l)
        {
            Len = 1;

            StringBuilder b = new StringBuilder();
            while (true)
            {
                char c = l[l.Position + Len];
                if (c == '\0')
                {
                    string s1 = l.Substring(l.Position, Len);
                    return new UnfinishedString(s1);
                }
                if (c == '"')
                {
                    char nextC = l[l.Position + Len+1];
                    if (nextC == '"')
                    {
                        Len++;
                        b.Append('"');
                    }
                    else
                    {
                        Len++;
                        string s1 ;//\\= l.Substring(l.Position, Len);
                        string s2 = l.Substring(l.Position, Len);
                        s1 = b.ToString();
                        //	string s2 = System.Text.RegularExpressions.Regex.
                        return new ExplicitString(s1, s2);
                    }
                }
                else
                {
                    {
                        b.Append(c);
                    }
                }
                Len++;
            }
            //throw new Exception("Invalid explicit string");
        }
        int Len;
        public override int MatchSize()
        {
            return Len;
        }
    }



}
